<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
use Excel;
use DB;
use App\Models\Order;
class ExcelController extends Controller
{
    //
    /**
     *
     * Excel导出
     */
    public function export()
    {
        ini_set('memory_limit','500M');
        set_time_limit(0);//设置超时限制为0分钟
        $cellData = Order::select('openid','name','out_trade_no','price','paytime','phone','phone_code')->limit(5)->get()->toArray();
        
      
        
        /*$cellData[0] = array('用户ID','名称','订单号','价格','付款时间','手机号','订单短信验证码');
        
        
        for($i=0;$i<count($cellData);$i++){
            $cellData[$i] = array_values($cellData[$i]);
            $cellData[$i][0] = str_replace('=',' '.'=',$cellData[$i][0]);
        }*/
        
        
       array_unshift($cellData,array('用户ID','名称','订单号','价格','付款时间','手机号','订单短信验证码'));  
        for($i=0;$i<count($cellData);$i++){  
            $cellData[$i] = array_values($cellData[$i]);  
            $cellData[$i][0] = str_replace('=',' '.'=',$cellData[$i][0]);  
        } 
        
        
        //dd($cellData);
        Excel::create('小程序订单信息',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        die;
    }

}
